package com.dayatang.android.Activity;

import java.util.concurrent.ExecutionException;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;

import com.dayatang.android.Chart.AbstractChart;

public class DivisionExpenditureActivity extends AbstractChart {

	private String[] divisionName = null;
	private String[] divisionExpenditure = null;
	
	protected void InitData(){
		String url = getServerAddress() + "/remote/division-expenditure.action?fromDate="+fromDate+"&toDate="+toDate;
		try {
			String result = CheckServerData(url);
			if(result != null){
				JSONArray array = new JSONArray(result);
				divisionName = new String[array.length()];
				divisionExpenditure = new String[array.length()];
				for (int i = 0, len = array.length(); i < len; i++) {
					JSONObject jsonObject = array.getJSONObject(i);
					divisionName[i] = jsonObject.getString("divisionName");
					divisionExpenditure[i] = jsonObject.getString("divisionExpenditure");
				}
				FillContentView(execute());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
	
	public View execute() {
	    XYMultipleSeriesRenderer renderer = BuildBarRenderer(getResources().getColor(R.color.report_six));
	    setChartSettings(renderer,getString(R.string.reports_tab_seven)+getString(R.string.reports_statistics),getString(R.string.reports_x_axis_division),getString(R.string.reports_y_axis_expenditure),0.5,8.5,0,getArrayMax(divisionExpenditure)*1.1);
	    for (int i = 1, len = divisionExpenditure.length; i <= len; i++) {
	    	renderer.addXTextLabel(i, divisionName[i-1]);
	    }
		return ChartFactory.getBarChartView(this, BuildBarDataset(divisionName,divisionExpenditure), renderer, Type.DEFAULT);
	}

}
